package com.ytsakura.blog.web;

import com.ytsakura.blog.pojo.Message;
import com.ytsakura.blog.pojo.User;
import com.ytsakura.blog.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;




@Controller
public class MessageController {

    @Autowired
    private MessageService messageService;

    private static final String PAGE_SIZE ="10";

    @Value("${comment.avatar}")
    private String avatar;

//    @Value("${admin.openid}")
//    private String adminOpenid;

    @GetMapping("/messageBoard")
    public String messageBoard(){
        return "message_board";
    }

    @PostMapping("/POSTMessages")
    public String post(Message message, HttpSession session) {
        User user=(User)session.getAttribute("user");
//        if(user != null){
//            String avatar = (String) session.getAttribute("avatar");
//            if(user.getType()==1){
//                message.setAvatar(avatar);
//                message.setAdminMessage(2);
//            }else{
//                message.setAvatar(user.getAvatar());
//                message.setAdminMessage(1);
//            }
//        }else{
//            message.setAdminMessage(0);
//            message.setAvatar(avatar);
//        }
        if (user != null){

            message.setAdminMessage(user.getType());
            message.setAvatar(user.getAvatar());
            message.setNickname(user.getNickname());
        }else{
            message.setAvatar(avatar);
        }
        messageService.saveMessage(message);
        messageService.findParentMessage(message);

        return "redirect:/Message";
    }

    @RequestMapping(value = "/Messages",method = RequestMethod.GET)
    public String Message_board(@RequestParam(value = "key",required = false)String key,
                                @RequestParam(value = "page",defaultValue = "1")Integer page,
                                @RequestParam(value = "rows",defaultValue = "8")Integer rows,
                                @RequestParam(value = "sortBy",defaultValue = "createTime")String sortBy,
                                @RequestParam(value = "desc",required = false)Boolean desc,
                                Model model){
        Pageable pageable = PageRequest.of(page-1, rows, Sort.Direction.DESC,sortBy);

        Page<Message> messages = messageService.listMessage(pageable);
        boolean a = messages.hasPrevious();
        boolean b =messages.hasNext();
        model.addAttribute("a",a);//判断是否为首页
        model.addAttribute("b",b);//判断是否为尾页
        model.addAttribute("messages",messageService.listMessage(pageable));
        return "message_board";
    }

    @RequestMapping(value = "/Message",method = RequestMethod.GET)
    public String message(@RequestParam(value = "key",required = false)String key,
                          @RequestParam(value = "page",defaultValue = "1")Integer page,
                          @RequestParam(value = "rows",defaultValue = "8")Integer rows,
                          @RequestParam(value = "sortBy",defaultValue = "createTime")String sortBy,
                          @RequestParam(value = "desc",required = false)Boolean desc,
                          Model model){
        Pageable pageable =PageRequest.of(page-1,rows,Sort.Direction.DESC,sortBy);
        Page<Message> messages = messageService.listMessage(pageable);
        boolean a = messages.hasPrevious();
        boolean b =messages.hasNext();
        model.addAttribute("a",a);//判断是否为首页
        model.addAttribute("b",b);//判断是否为尾页
        model.addAttribute("messages",messageService.listMessage(pageable));
        return "message_board :: messageList";//将数据返回message_board页面的th:fragment="messageList"片段，实现局部刷新
    }
}
